package com.example.bms.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bms.entity.ApiResult;
import com.example.bms.entity.Message;
import com.example.bms.serviceimpl.MessageServiceImpl;
import com.example.bms.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @apiNote 用于实现对留言的增删改查
 * @author 任玥越
 * @email 3108289016@qq.com
 * @since 1.0.0
 */
@RestController
@CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.POST,RequestMethod.PUT,RequestMethod.DELETE})
public class MessageController {

    @Autowired
    private MessageServiceImpl messageService;


    /**
     * @apiNote 查询所有留言
     * @param page 页数
     * @param size 条数
     * @author 任玥越
     * @email 3108289016@qq.com
     * @return ApiResult
     * @since 1.0.0
     */
    @GetMapping("/messages/{page}/{size}")
    public ApiResult<Message> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        System.out.println(page);
        System.out.println(size);
        Page<Message> messagePage = new Page<>(page,size);
        IPage<Message> all = messageService.findAll(messagePage);
        return ApiResultHandler.buildApiResult(200,"查询所有留言",all);
    }


    /**
     * @apiNote 根据id查询留言
     * @param id 留言Id
     * @author 任玥越
     * @email 3108289016@qq.com
     * @return ApiResult
     * @since 1.0.0
     */
    @GetMapping("/message/{id}")
    public ApiResult findById(@PathVariable("id") Integer id) {
        Message res = messageService.findById(id);
        return ApiResultHandler.buildApiResult(200,"根据Id查询",res);
    }


    /**
     * @apiNote 根据id删除留言
     * @param id 留言Id
     * @author 任玥越
     * @email 3108289016@qq.com
     * @return ApiResult
     * @since 1.0.0
     */
    @DeleteMapping("/message/{id}")
    public int delete(@PathVariable("id") Integer id) {
        int res = messageService.delete(id);
        return res;
    }


    /**
     * @apiNote 增加留言
     * @param message 留言实体类
     * @author 任玥越
     * @email 3108289016@qq.com
     * @return ApiResult
     * @since 1.0.0
     */
    @PostMapping("/message")
    public ApiResult add(@RequestBody Message message) {
        System.out.println(message.getContent());
        System.out.println(message.getTitle());
        System.out.println("123123");
        int res = messageService.add(message);
        if (res == 0) {
            return ApiResultHandler.buildApiResult(400,"添加失败",res);
        } else {
            return ApiResultHandler.buildApiResult(200,"添加成功",res);
        }
    }
}
